VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Object = "{BDC217C8-ED16-11CD-956C-0000C04E4C0A}#1.1#0"; "TABCTL32.OCX"
Object = "{CDE57A40-8B86-11D0-B3C6-00A0C90AEA82}#1.0#0"; "MSDATGRD.OCX"
Object = "{B3FB64BF-91F9-11D7-A482-0008A14158BC}#2.22#0"; "ITGControls.ocx"
Begin VB.Form frmMaintCommissionCriteria 
   BorderStyle     =   0  'None
   Caption         =   "Commission Criteria"
   ClientHeight    =   5070
   ClientLeft      =   0
   ClientTop       =   -45
   ClientWidth     =   10230
   BeginProperty Font 
      Name            =   "Tahoma"
      Size            =   8.25
      Charset         =   0
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   Icon            =   "frmMaintCommissionCriteria.frx":0000
   KeyPreview      =   -1  'True
   LinkTopic       =   "Form1"
   LockControls    =   -1  'True
   MaxButton       =   0   'False
   MDIChild        =   -1  'True
   ScaleHeight     =   5070
   ScaleWidth      =   10230
   ShowInTaskbar   =   0   'False
   Begin TabDlg.SSTab SSTab1 
      Height          =   4695
      Left            =   60
      TabIndex        =   5
      Top             =   60
      Width           =   10095
      _ExtentX        =   17806
      _ExtentY        =   8281
      _Version        =   393216
      Tabs            =   2
      TabsPerRow      =   2
      TabHeight       =   520
      TabMaxWidth     =   3528
      BackColor       =   16243138
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Tahoma"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      TabCaption(0)   =   "Main"
      TabPicture(0)   =   "frmMaintCommissionCriteria.frx":0CCA
      Tab(0).ControlEnabled=   -1  'True
      Tab(0).Control(0)=   "Frame1"
      Tab(0).Control(0).Enabled=   0   'False
      Tab(0).ControlCount=   1
      TabCaption(1)   =   "List"
      TabPicture(1)   =   "frmMaintCommissionCriteria.frx":0CE6
      Tab(1).ControlEnabled=   0   'False
      Tab(1).Control(0)=   "dtgList"
      Tab(1).ControlCount=   1
      Begin VB.Frame Frame1 
         Height          =   4200
         Left            =   120
         TabIndex        =   6
         Top             =   360
         Width           =   9840
         Begin VB.ComboBox cboType 
            Appearance      =   0  'Flat
            BackColor       =   &H00FFFFFF&
            Height          =   315
            ItemData        =   "frmMaintCommissionCriteria.frx":0D02
            Left            =   7920
            List            =   "frmMaintCommissionCriteria.frx":0D0F
            Style           =   2  'Dropdown List
            TabIndex        =   2
            Top             =   660
            Width           =   1605
         End
         Begin VB.Timer Timer1 
            Interval        =   300
            Left            =   2820
            Top             =   180
         End
         Begin MSDataGridLib.DataGrid dtgApplyTo 
            Height          =   1935
            Left            =   4620
            TabIndex        =   4
            Top             =   1380
            Width           =   4875
            _ExtentX        =   8599
            _ExtentY        =   3413
            _Version        =   393216
            AllowUpdate     =   0   'False
            AllowArrows     =   -1  'True
            Appearance      =   0
            HeadLines       =   1
            RowHeight       =   15
            TabAction       =   2
            FormatLocked    =   -1  'True
            BeginProperty HeadFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
               Name            =   "Tahoma"
               Size            =   8.25
               Charset         =   0
               Weight          =   400
               Underline       =   0   'False
               Italic          =   0   'False
               Strikethrough   =   0   'False
            EndProperty
            BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
               Name            =   "Tahoma"
               Size            =   8.25
               Charset         =   0
               Weight          =   400
               Underline       =   0   'False
               Italic          =   0   'False
               Strikethrough   =   0   'False
            EndProperty
            Caption         =   "Apply To"
            ColumnCount     =   2
            BeginProperty Column00 
               DataField       =   "cID"
               Caption         =   "ID"
               BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
                  Type            =   0
                  Format          =   ""
                  HaveTrueFalseNull=   0
                  FirstDayOfWeek  =   0
                  FirstWeekOfYear =   0
                  LCID            =   1033
                  SubFormatType   =   0
               EndProperty
            EndProperty
            BeginProperty Column01 
               DataField       =   "cDesc"
               Caption         =   "Description/Name"
               BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
                  Type            =   0
                  Format          =   ""
                  HaveTrueFalseNull=   0
                  FirstDayOfWeek  =   0
                  FirstWeekOfYear =   0
                  LCID            =   1033
                  SubFormatType   =   0
               EndProperty
            EndProperty
            SplitCount      =   1
            BeginProperty Split0 
               AllowRowSizing  =   0   'False
               BeginProperty Column00 
                  ColumnAllowSizing=   -1  'True
                  Button          =   -1  'True
                  ColumnWidth     =   1395.213
               EndProperty
               BeginProperty Column01 
                  Locked          =   -1  'True
                  ColumnWidth     =   2970.142
               EndProperty
            EndProperty
         End
         Begin ITGControls.ITGTextBox txtComID 
            Height          =   285
            Left            =   240
            TabIndex        =   0
            Top             =   300
            Width           =   2565
            _ExtentX        =   4313
            _ExtentY        =   503
            SendKeysTab     =   -1  'True
            BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
               Name            =   "Tahoma"
               Size            =   8.25
               Charset         =   0
               Weight          =   400
               Underline       =   0   'False
               Italic          =   0   'False
               Strikethrough   =   0   'False
            EndProperty
            AllCaps         =   -1  'True
            Mandatory       =   -1  'True
            Label           =   "Commission ID"
            BeginProperty LabelFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
               Name            =   "Tahoma"
               Size            =   8.25
               Charset         =   0
               Weight          =   400
               Underline       =   0   'False
               Italic          =   0   'False
               Strikethrough   =   0   'False
            EndProperty
            LabelWidth      =   1250
            TextBoxWidth    =   1255
            Required        =   -1  'True
         End
         Begin ITGControls.ITGTextBox txtDescription 
            Height          =   285
            Left            =   240
            TabIndex        =   1
            Top             =   660
            Width           =   6945
            _ExtentX        =   12039
            _ExtentY        =   503
            SendKeysTab     =   -1  'True
            BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
               Name            =   "Tahoma"
               Size            =   8.25
               Charset         =   0
               Weight          =   400
               Underline       =   0   'False
               Italic          =   0   'False
               Strikethrough   =   0   'False
            EndProperty
            Label           =   "Description"
            BeginProperty LabelFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
               Name            =   "Tahoma"
               Size            =   8.25
               Charset         =   0
               Weight          =   400
               Underline       =   0   'False
               Italic          =   0   'False
               Strikethrough   =   0   'False
            EndProperty
            LabelWidth      =   1250
            TextBoxWidth    =   5635
            Required        =   -1  'True
         End
         Begin ITGControls.ITGLabel ITGLabel11 
            Height          =   285
            Left            =   7380
            TabIndex        =   10
            Top             =   660
            Width           =   555
            _ExtentX        =   979
            _ExtentY        =   503
            Caption         =   "Type"
            BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
               Name            =   "Tahoma"
               Size            =   8.25
               Charset         =   0
               Weight          =   400
               Underline       =   0   'False
               Italic          =   0   'False
               Strikethrough   =   0   'False
            EndProperty
         End
         Begin ITGControls.ITGTextBox txtType 
            Height          =   285
            Left            =   8340
            TabIndex        =   11
            Tag             =   "cboType"
            Top             =   420
            Visible         =   0   'False
            Width           =   1140
            _ExtentX        =   1905
            _ExtentY        =   503
            BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
               Name            =   "Tahoma"
               Size            =   8.25
               Charset         =   0
               Weight          =   400
               Underline       =   0   'False
               Italic          =   0   'False
               Strikethrough   =   0   'False
            EndProperty
            Mandatory       =   -1  'True
            Label           =   "Transaction Type"
            BeginProperty LabelFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
               Name            =   "Tahoma"
               Size            =   8.25
               Charset         =   0
               Weight          =   400
               Underline       =   0   'False
               Italic          =   0   'False
               Strikethrough   =   0   'False
            EndProperty
            LabelWidth      =   0
            TextBoxWidth    =   1080
         End
         Begin MSDataGridLib.DataGrid dtgCriteria 
            Height          =   1935
            Left            =   300
            TabIndex        =   3
            Top             =   1380
            Width           =   3915
            _ExtentX        =   6906
            _ExtentY        =   3413
            _Version        =   393216
            AllowUpdate     =   0   'False
            AllowArrows     =   -1  'True
            Appearance      =   0
            HeadLines       =   1
            RowHeight       =   15
            TabAction       =   2
            FormatLocked    =   -1  'True
            BeginProperty HeadFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
               Name            =   "Tahoma"
               Size            =   8.25
               Charset         =   0
               Weight          =   400
               Underline       =   0   'False
               Italic          =   0   'False
               Strikethrough   =   0   'False
            EndProperty
            BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
               Name            =   "Tahoma"
               Size            =   8.25
               Charset         =   0
               Weight          =   400
               Underline       =   0   'False
               Italic          =   0   'False
               Strikethrough   =   0   'False
            EndProperty
            Caption         =   "Commission Criteria"
            ColumnCount     =   4
            BeginProperty Column00 
               DataField       =   "nFrom"
               Caption         =   "From"
               BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
                  Type            =   1
                  Format          =   "#,##0.00"
                  HaveTrueFalseNull=   0
                  FirstDayOfWeek  =   0
                  FirstWeekOfYear =   0
                  LCID            =   1033
                  SubFormatType   =   1
               EndProperty
            EndProperty
            BeginProperty Column01 
               DataField       =   "nTo"
               Caption         =   "To"
               BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
                  Type            =   1
                  Format          =   "#,##0.00"
                  HaveTrueFalseNull=   0
                  FirstDayOfWeek  =   0
                  FirstWeekOfYear =   0
                  LCID            =   1033
                  SubFormatType   =   1
               EndProperty
            EndProperty
            BeginProperty Column02 
               DataField       =   "cOperator"
               Caption         =   "Operator"
               BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
                  Type            =   0
                  Format          =   ""
                  HaveTrueFalseNull=   0
                  FirstDayOfWeek  =   0
                  FirstWeekOfYear =   0
                  LCID            =   1033
                  SubFormatType   =   0
               EndProperty
            EndProperty
            BeginProperty Column03 
               DataField       =   "nRate"
               Caption         =   "Rate (%)"
               BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
                  Type            =   1
                  Format          =   "#,##0.00"
                  HaveTrueFalseNull=   0
                  FirstDayOfWeek  =   0
                  FirstWeekOfYear =   0
                  LCID            =   1033
                  SubFormatType   =   1
               EndProperty
            EndProperty
            SplitCount      =   1
            BeginProperty Split0 
               AllowRowSizing  =   0   'False
               BeginProperty Column00 
                  Alignment       =   1
                  ColumnWidth     =   794.835
               EndProperty
               BeginProperty Column01 
                  Alignment       =   1
                  ColumnWidth     =   794.835
               EndProperty
               BeginProperty Column02 
                  ColumnWidth     =   794.835
               EndProperty
               BeginProperty Column03 
                  Alignment       =   1
                  ColumnWidth     =   1005.165
               EndProperty
            EndProperty
         End
         Begin VB.Label lblApplyTo 
            Height          =   555
            Left            =   4620
            TabIndex        =   13
            Top             =   3480
            Width           =   4875
         End
         Begin VB.Label lblCriteria 
            Height          =   555
            Left            =   300
            TabIndex        =   12
            Top             =   3480
            Width           =   3915
         End
         Begin VB.Label Label4 
            AutoSize        =   -1  'True
            BackStyle       =   0  'Transparent
            Caption         =   "Details"
            BeginProperty Font 
               Name            =   "Tahoma"
               Size            =   8.25
               Charset         =   0
               Weight          =   700
               Underline       =   0   'False
               Italic          =   0   'False
               Strikethrough   =   0   'False
            EndProperty
            ForeColor       =   &H00000000&
            Height          =   195
            Index           =   2
            Left            =   120
            TabIndex        =   7
            Top             =   1020
            Width           =   585
         End
         Begin VB.Line Line2 
            BorderColor     =   &H00000000&
            X1              =   780
            X2              =   9720
            Y1              =   1140
            Y2              =   1140
         End
      End
      Begin MSDataGridLib.DataGrid dtgList 
         Height          =   4125
         Left            =   -74880
         TabIndex        =   8
         Top             =   435
         Width           =   9840
         _ExtentX        =   17357
         _ExtentY        =   7276
         _Version        =   393216
         AllowUpdate     =   0   'False
         AllowArrows     =   -1  'True
         Appearance      =   0
         HeadLines       =   1
         RowHeight       =   15
         TabAction       =   2
         FormatLocked    =   -1  'True
         BeginProperty HeadFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "Tahoma"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "Tahoma"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ColumnCount     =   3
         BeginProperty Column00 
            DataField       =   "cComID"
            Caption         =   "Commission ID"
            BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
               Type            =   0
               Format          =   ""
               HaveTrueFalseNull=   0
               FirstDayOfWeek  =   0
               FirstWeekOfYear =   0
               LCID            =   1033
               SubFormatType   =   0
            EndProperty
         EndProperty
         BeginProperty Column01 
            DataField       =   "cDescription"
            Caption         =   "Description"
            BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
               Type            =   0
               Format          =   ""
               HaveTrueFalseNull=   0
               FirstDayOfWeek  =   0
               FirstWeekOfYear =   0
               LCID            =   1033
               SubFormatType   =   0
            EndProperty
         EndProperty
         BeginProperty Column02 
            DataField       =   "cType"
            Caption         =   "Type"
            BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
               Type            =   0
               Format          =   "#,##0.00"
               HaveTrueFalseNull=   0
               FirstDayOfWeek  =   0
               FirstWeekOfYear =   0
               LCID            =   1033
               SubFormatType   =   0
            EndProperty
         EndProperty
         SplitCount      =   1
         BeginProperty Split0 
            AllowRowSizing  =   0   'False
            BeginProperty Column00 
               ColumnAllowSizing=   -1  'True
               Locked          =   -1  'True
               ColumnWidth     =   1620.284
            EndProperty
            BeginProperty Column01 
               Locked          =   -1  'True
               ColumnWidth     =   5850.142
            EndProperty
            BeginProperty Column02 
               Locked          =   -1  'True
               ColumnWidth     =   1844.787
            EndProperty
         EndProperty
      End
   End
   Begin MSComctlLib.StatusBar sbRS 
      Align           =   2  'Align Bottom
      Height          =   255
      Left            =   0
      TabIndex        =   9
      Top             =   4815
      Width           =   10230
      _ExtentX        =   18045
      _ExtentY        =   450
      _Version        =   393216
      BeginProperty Panels {8E3867A5-8586-11D1-B16A-00C0F0283628} 
         NumPanels       =   2
         BeginProperty Panel1 {8E3867AB-8586-11D1-B16A-00C0F0283628} 
            Object.Width           =   3528
            MinWidth        =   3528
         EndProperty
         BeginProperty Panel2 {8E3867AB-8586-11D1-B16A-00C0F0283628} 
         EndProperty
      EndProperty
   End
End
Attribute VB_Name = "frmMaintCommissionCriteria"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'IT Group Inc. 2005.09.23

Option Explicit

'Object variables





'Recordset variables
Private WithEvents rsHeader As ADODB.Recordset
Attribute rsHeader.VB_VarHelpID = -1
Private rsCriteria As ADODB.Recordset
Private rsApplyTo As ADODB.Recordset

'ADO Connection variables

Private connHeader As ADODB.Connection
Private connCriteria As ADODB.Connection
Private connApplyTo As ADODB.Connection

'Form mode enumeration
Public Enum eComCMode
    Normal
    AddNewEdit
    Find
End Enum
Private Mode As eComCMode

'Other declaration
Public dtgName As String
Public sBit As String
Private vBM As Variant 'Recordset bookmark variable

'Security Acess Level variables
Public lACNew As Boolean
Public lACEdit As Boolean
Public lACDelete As Boolean
Public lACPost As Boolean
Public lACCancel As Boolean
Public lACPrint As Boolean

Private Sub cboType_Click()
    txtType = Trim(cboType)
    If Trim(txtType) = "Terms" Then
        lblCriteria.Caption = "'From' && 'To' are terms given to the customer client/s."
        lblApplyTo.Caption = "The commission criteria is applied to the client/s on the list."
    ElseIf Trim(txtType) = "Quota" Then
        lblCriteria.Caption = "'From' && 'To' are percentage of the salesman's quota."
        lblApplyTo.Caption = "The commission criteria is applied to the saleman/salesmen on the list."
    ElseIf Trim(txtType) = "Profit Margin" Then
        lblCriteria.Caption = "'From' && 'To' are percentage of the profit margin per product."
        lblApplyTo.Caption = "The commission criteria is applied to the product/s on the list."
    Else
        lblCriteria.Caption = ""
        lblApplyTo.Caption = ""
    End If
End Sub

Private Sub cboType_KeyPress(KeyAscii As Integer)
    SendKeysTab KeyAscii
End Sub

'After column update
Private Sub dtgApplyTo_AfterColUpdate(ByVal ColIndex As Integer)
    If Mode <> AddNewEdit Then Exit Sub
    Select Case ColIndex
        Case 0
            If Trim(txtType) = "Profit Margin" Then
                sSQL = "SELECT cItemNo, cDesc FROM ITEM WHERE cCompanyID = '" & Trim(COID) & "' " & _
                        "AND cItemNo IN (SELECT A.cID FROM COMMISSION_APPLYTO A LEFT OUTER JOIN COMMISSION B ON " & _
                        "A.cCompanyID = B.cCompanyID AND A.cComID = B.cComID WHERE B.cType = 'Profit Margin') " & _
                        "AND cItemNo = '" & Trim(dtgApplyTo.Columns(0).Value) & "'"
                Set rs = New Recordset
                rs.Open sSQL, cn, adOpenForwardOnly, adLockReadOnly
                If rs.RecordCount > 0 Then
                    Set rs = Nothing
                    Exit Sub
                End If
                Set rs = Nothing
                If GetValueFrTable("cItemNo", "ITEM", "cItemNo = '" & Trim(dtgApplyTo.Columns(0).Value) & "'") <> "" Then
                    dtgApplyTo.Columns(1).Text = GetValueFrTable("cDesc", "ITEM", "cItemNo = '" & Trim(dtgApplyTo.Columns(0).Value) & "'")
                End If
            ElseIf Trim(txtType) = "Terms" Then
                sSQL = "SELECT cCode,cName FROM CLIENT_CUSTOMER WHERE cCompanyID = '" & Trim(COID) & "' " & _
                        "AND cCode IN (SELECT A.cID FROM COMMISSION_APPLYTO A LEFT OUTER JOIN COMMISSION B ON " & _
                        "A.cCompanyID = B.cCompanyID AND A.cComID = B.cComID WHERE B.cType = 'Terms') " & _
                        "AND cCode = '" & Trim(dtgApplyTo.Columns(0).Value) & "'"
                Set rs = New Recordset
                rs.Open sSQL, cn, adOpenForwardOnly, adLockReadOnly
                If rs.RecordCount > 0 Then
                    Set rs = Nothing
                    Exit Sub
                End If
                Set rs = Nothing
                If GetValueFrTable("cCode", "CLIENT_CUSTOMER", "cCode = '" & Trim(dtgApplyTo.Columns(0).Value) & "'") <> "" Then
                    dtgApplyTo.Columns(1).Text = GetValueFrTable("cName", "CLIENT_CUSTOMER", "cCode = '" & Trim(dtgApplyTo.Columns(0).Value) & "'")
                End If
            ElseIf Trim(txtType) = "Quota" Then
                sSQL = "SELECT cCode,cName FROM SALESMAN WHERE cCompanyID = '" & Trim(COID) & "' " & _
                        "AND cCode IN (SELECT A.cID FROM COMMISSION_APPLYTO A LEFT OUTER JOIN COMMISSION B ON " & _
                        "A.cCompanyID = B.cCompanyID AND A.cComID = B.cComID WHERE B.cType = 'Quota') " & _
                        "AND cCode = '" & Trim(dtgApplyTo.Columns(0).Value) & "'"
                Set rs = New Recordset
                rs.Open sSQL, cn, adOpenForwardOnly, adLockReadOnly
                If rs.RecordCount > 0 Then
                    Set rs = Nothing
                    Exit Sub
                End If
                Set rs = Nothing
                If GetValueFrTable("cCode", "SALESMAN", "cCode = '" & Trim(dtgApplyTo.Columns(0).Value) & "'") <> "" Then
                    dtgApplyTo.Columns(1).Text = GetValueFrTable("cName", "SALESMAN", "cCode = '" & Trim(dtgApplyTo.Columns(0).Value) & "'")
                End If
            End If
    End Select
End Sub

Private Sub dtgApplyTo_ButtonClick(ByVal ColIndex As Integer)
    If Mode <> AddNewEdit Then Exit Sub
    Select Case ColIndex
        Case 0
            If lPickListActive Then Exit Sub
            sFilterString = Trim(dtgApplyTo.Columns(0).Text)
            RepName = "CommissionCriteria"
            If Trim(txtType) = "Profit Margin" Then
                frmITGPickList.mType = Product
            ElseIf Trim(txtType) = "Terms" Then
                frmITGPickList.mType = Client_Customer
            ElseIf Trim(txtType) = "Quota" Then
                frmITGPickList.mType = Salesman
            End If
            frmITGPickList.Show vbModal
    End Select
End Sub

Private Sub dtgApplyTo_Click()
    If Mode = AddNewEdit Then dtgName = dtgApplyTo.Name
End Sub

Private Sub dtgApplyTo_Error(ByVal DataError As Integer, Response As Integer)
    Response = 0
End Sub

Private Sub dtgApplyTo_GotFocus()
    dtgName = dtgApplyTo.Name
    If Mode <> AddNewEdit Then Exit Sub
    dtgApplyTo.HeadFont.Bold = True
End Sub

Private Sub dtgApplyTo_KeyDown(KeyCode As Integer, Shift As Integer)
    If Mode <> AddNewEdit Then Exit Sub
    If (Shift = vbCtrlMask And KeyCode = 45) Then
        TBNewLine
    ElseIf (Shift = vbCtrlMask And KeyCode = 46) Then
        TBDeleteLine
    ElseIf (Shift = vbCtrlMask And KeyCode = 83) Then
        TBSave
    End If
End Sub

Private Sub dtgApplyTo_KeyPress(KeyAscii As Integer)
    
    If Mode <> AddNewEdit Then Exit Sub
    If rsApplyTo.RecordCount = 0 Then Exit Sub
        
    If KeyAscii = 39 Then KeyAscii = 0 'Apostrophe {'}
    
    If KeyAscii = 13 Then
        Select Case dtgApplyTo.Col
            Case 0
                If Trim(txtType) = "Profit Margin" Then
                    sSQL = "SELECT cItemNo, cDesc FROM ITEM WHERE cCompanyID = '" & Trim(COID) & "' " & _
                            "AND cItemNo IN (SELECT A.cID FROM COMMISSION_APPLYTO A LEFT OUTER JOIN COMMISSION B ON " & _
                            "A.cCompanyID = B.cCompanyID AND A.cComID = B.cComID WHERE B.cType = 'Profit Margin') " & _
                            "AND cItemNo = '" & Trim(dtgApplyTo.Columns(0).Value) & "'"
                    Set rs = New Recordset
                    rs.Open sSQL, cn, adOpenForwardOnly, adLockReadOnly
                    If rs.RecordCount > 0 Then
                        TBDeleteLine
                        dtgApplyTo_ButtonClick (0) 'use button click
                    Else
                        TBNewLine
                    End If
                    Set rs = Nothing
                ElseIf Trim(txtType) = "Terms" Then
                    sSQL = "SELECT cCode,cName FROM CLIENT_CUSTOMER WHERE cCompanyID = '" & Trim(COID) & "' " & _
                            "AND cCode IN (SELECT A.cID FROM COMMISSION_APPLYTO A LEFT OUTER JOIN COMMISSION B ON " & _
                            "A.cCompanyID = B.cCompanyID AND A.cComID = B.cComID WHERE B.cType = 'Terms') " & _
                            "AND cCode = '" & Trim(dtgApplyTo.Columns(0).Value) & "'"
                    Set rs = New Recordset
                    rs.Open sSQL, cn, adOpenForwardOnly, adLockReadOnly
                    If rs.RecordCount > 0 Then
                        TBDeleteLine
                        dtgApplyTo_ButtonClick (0) 'use button click
                    Else
                        TBNewLine
                    End If
                    Set rs = Nothing
                ElseIf Trim(txtType) = "Quota" Then
                    sSQL = "SELECT cCode,cName FROM SALESMAN WHERE cCompanyID = '" & Trim(COID) & "' " & _
                            "AND cCode IN (SELECT A.cID FROM COMMISSION_APPLYTO A LEFT OUTER JOIN COMMISSION B ON " & _
                            "A.cCompanyID = B.cCompanyID AND A.cComID = B.cComID WHERE B.cType = 'Quota') " & _
                            "AND cCode = '" & Trim(dtgApplyTo.Columns(0).Value) & "'"
                    Set rs = New Recordset
                    rs.Open sSQL, cn, adOpenForwardOnly, adLockReadOnly
                    If rs.RecordCount > 0 Then
                        TBDeleteLine
                        dtgApplyTo_ButtonClick (0) 'use button click
                    Else
                        TBNewLine
                    End If
                    Set rs = Nothing
                End If
            Case Else
                SendKeys "{Tab}"
        End Select
    Else
        KeyAscii = Asc(UCase(Chr(KeyAscii))) 'All Upper Case String
    End If
    
    
End Sub

Private Sub dtgApplyTo_LostFocus()
    dtgApplyTo.HeadFont.Bold = False
End Sub

'Right click menu popup
Private Sub dtgApplyTo_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Mode <> AddNewEdit Then Exit Sub
    If Button = 2 Then
        dtgName = dtgApplyTo.Name
        PopupMenu ITGLedgerMain.mnuDetail
    End If
End Sub

Private Sub dtgCriteria_Click()
    If Mode = AddNewEdit Then dtgName = dtgCriteria.Name
End Sub

Private Sub dtgCriteria_Error(ByVal DataError As Integer, Response As Integer)
    Response = 0
End Sub

Private Sub dtgCriteria_GotFocus()
    dtgName = dtgCriteria.Name
    If Mode <> AddNewEdit Then Exit Sub
    dtgCriteria.HeadFont.Bold = True
End Sub

Private Sub dtgCriteria_KeyDown(KeyCode As Integer, Shift As Integer)
    If Mode <> AddNewEdit Then Exit Sub
    If (Shift = vbCtrlMask And KeyCode = 45) Then
        TBNewLine
    ElseIf (Shift = vbCtrlMask And KeyCode = 46) Then
        TBDeleteLine
    ElseIf (Shift = vbCtrlMask And KeyCode = 83) Then
        TBSave
    End If
End Sub

Private Sub dtgCriteria_KeyPress(KeyAscii As Integer)
    
    If Mode <> AddNewEdit Then Exit Sub
    If rsCriteria.RecordCount = 0 Then Exit Sub
        
    If KeyAscii = 39 Then KeyAscii = 0 'Apostrophe {'}
    
    If KeyAscii = 13 Then
        Select Case dtgCriteria.Col
            Case 3
                TBNewLine
                dtgCriteria.Col = 0
            Case Else
                SendKeys "{Tab}"
        End Select
    Else
        KeyAscii = Asc(UCase(Chr(KeyAscii))) 'All Upper Case String
        If KeyAscii = 9 Then Exit Sub
        Select Case dtgCriteria.Col
            Case 0
                KeyAscii = ValidKeys(KeyAscii, "1234567890.", True)
            Case 1
                KeyAscii = ValidKeys(KeyAscii, "1234567890.", True)
            Case 2
                KeyAscii = FixedLength(KeyAscii, Trim(dtgCriteria.Columns(2).Text), 1)
                KeyAscii = ValidKeys(KeyAscii, "AD", True)
            Case 3
                KeyAscii = ValidKeys(KeyAscii, "1234567890.", True)
        End Select
    End If
    
    
End Sub

Private Sub dtgCriteria_LostFocus()
    dtgCriteria.HeadFont.Bold = False
End Sub

'Right click menu popup
Private Sub dtgCriteria_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Mode <> AddNewEdit Then Exit Sub
    If Button = 2 Then
        dtgName = dtgCriteria.Name
        PopupMenu ITGLedgerMain.mnuDetail
    End If
End Sub

'Set Your Object
Private Sub Form_Load()
        
    Set FrmName = Me
    FormSetup
    
    AcessBit Me, GetValueFrTable("AccessLevel", "SEC_ACCESSLEVEL", "RoleID = '" & SecUserRole & "' AND [Module] = 'MF06'")
    
    Set rsHeader = New ADODB.Recordset
    Set rsCriteria = New ADODB.Recordset
    Set rsApplyTo = New ADODB.Recordset
    
    FormLocking True
    FormSearch True
    SearchObj True
    BitEnabled ITGLedgerMain, Me, ITGLedgerMain.tbrMain, lACNew, , , , , , , , , True, , , True
    BitVisible ITGLedgerMain.tbrMain, True
    ITGLedgerMain.tbrMain.Buttons("btnFind").ButtonMenus("btnFindP").Enabled = True

    Mode = Find
    txtComID.Locked = False
End Sub

'Activate your Toolbar Mode
Private Sub Form_Activate()
    TBBitReload
End Sub

'Release your Object
Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
    
    If Mode = AddNewEdit Then
        MsgBox "Unable to close. You are in Add/New/Edit mode." & vbCr & _
            " Must Save or Undo", vbCritical, Me.Caption
        Cancel = True
        Exit Sub
    End If
    
    CloseMenuTab ITGLedgerMain
    
    BitEnabled ITGLedgerMain, Me, ITGLedgerMain.tbrMain, , , , , , , , , , True
    BitVisible ITGLedgerMain.tbrMain
    ITGLedgerMain.tbrMain.Buttons("btnFind").ButtonMenus("btnFindP").Enabled = False
    
    
    
    
    
    Set rsHeader = Nothing
    Set rsCriteria = Nothing
    Set rsApplyTo = Nothing
    Set connHeader = Nothing
    Set connCriteria = Nothing
    Set connApplyTo = Nothing
    
    Set frmMaintCommissionCriteria = Nothing

    lCloseWindow = True
End Sub

'Add new record to the recordset
Public Sub TBNew()
    
    BitEnabled ITGLedgerMain, Me, ITGLedgerMain.tbrMain, , , , True, True, , , True, True, , , , True
    txtComID.BackColor = &HE0FFFF
    Mode = AddNewEdit
    
    If rsHeader.State <> adStateOpen Then
        OpenNewConnection connHeader
        OpenNewConnection connCriteria
        OpenNewConnection connApplyTo
        OpenRecordsetWithCN rsCriteria, "*", "COMMISSION_CRITERIA", connCriteria, "WHERE 1 = 0"
        OpenRecordsetWithCN rsApplyTo, "*", "COMMISSION_APPLYTO", connApplyTo, "WHERE 1 = 0"
        OpenRecordsetWithCN rsHeader, "*", "COMMISSION", connHeader, "WHERE 1 = 0"
        SetDataSource
        SetDataField
    Else
        vBM = rsHeader.Bookmark
    End If
    
    rsHeader.AddNew
    rsHeader!cCompanyID = COID
    rsHeader!cType = "Terms"
    
    Set FrmName = Me
    FormLocking False
    ClrRequired &HC0&
    SSTab1.Tab = 0
    txtComID.SetFocus

End Sub

'Undo all changes to the recordset
Public Sub TBUndoAll()
On Error GoTo ErrorHandler

    Mode = Normal
    
    If rsHeader.Status = adRecNew Then TBUndoCurrent
    
    rsHeader.CancelBatch adAffectAll
    rsCriteria.CancelBatch adAffectAll
    rsApplyTo.CancelBatch adAffectAll
    
    UnbindControls
    
    If rsHeader.RecordCount <> 0 Then rsHeader.Bookmark = vBM
    
    Set FrmName = Me
    FormLocking True
    
    If rsHeader.RecordCount <> 0 Then
        BitEnabled ITGLedgerMain, Me, ITGLedgerMain.tbrMain, lACNew, lACEdit, lACDelete, , , , , , , True, True, , True
    Else
        RSZero
        Mode = Find
    End If
    
    sbRS.Panels(2) = ""

    SetDataSource
    SetDataField

ErrorHandler:
    If err.Number = -2147217885 Then
        Resume Next
    ElseIf err.Number = -2147217842 Then 'Operation was cancelled. (Error returned by ITGDateBox)
        TBUndoAll
    End If

End Sub

'Undo changes on the current record
Public Sub TBUndoCurrent()
On Error GoTo ErrorHandler

    GetChild
    If rsHeader.Status = adRecNew Then
        rsCriteria.CancelBatch adAffectAll
        rsApplyTo.CancelBatch adAffectAll
        rsHeader.CancelUpdate
    Else
        rsHeader.CancelBatch adAffectCurrent
        rsCriteria.CancelBatch adAffectAll
        rsApplyTo.CancelBatch adAffectAll
    End If
   
    If rsHeader.RecordCount = 0 Then RSZero

ErrorHandler:
    If err.Number = -2147217885 Then
        Resume Next
    ElseIf err.Number = -2147217842 Then 'Operation was cancelled. (Error returned by ITGDateBox)
        TBUndoCurrent
    End If

End Sub

'Save all changes
Public Sub TBSave()
Dim OKUpdate As Boolean
On Error GoTo ErrHandler

    'Audit Trail
    lBoolean = False
    If rsHeader.Status = adRecNew Then lBoolean = True
    
    If Not MandatoryOK Then Exit Sub
    
    
    OKUpdate = False
    cn.BeginTrans
    connHeader.BeginTrans
    connCriteria.BeginTrans
    connApplyTo.BeginTrans
    
    rsHeader.UpdateBatch adAffectAll
    rsCriteria.UpdateBatch adAffectAll
    rsApplyTo.UpdateBatch adAffectAll
    
    cn.CommitTrans
    connHeader.CommitTrans
    connCriteria.CommitTrans
    connApplyTo.CommitTrans
    OKUpdate = True
    
    Set FrmName = Me
    FormLocking True
    BitEnabled ITGLedgerMain, Me, ITGLedgerMain.tbrMain, lACNew, lACEdit, lACDelete, , , , , , , True, True, , True
    Mode = Normal
    
    MsgBox "Record/s successfully saved.", vbInformation, "ComUnion"
    
    'Audit trail
    UpdateLogFile "Commission Criteria", Trim(txtComID), IIf(lBoolean, "Inserted", "Updated")
    
    sbRS.Panels(2) = ""

ErrHandler:
    If err.Number = -2147217885 Then
        Resume Next
    ElseIf err.Number = -2147217864 Then
        OKUpdate = True
        cn.RollbackTrans
        connHeader.RollbackTrans
        connCriteria.RollbackTrans
        connApplyTo.RollbackTrans
        MsgBox "Record cannot be updated. Some values may have been changed by other user/s since last read." & vbCr & _
                "Records will be automatically refreshed. All changes made to the record will be gone upon refresh.", vbInformation, "ComUnion"
        vBookMark = rsHeader.Bookmark
        UnbindControls
        rsHeader.Requery
        rsCriteria.Requery
        rsApplyTo.Requery
        BitEnabled ITGLedgerMain, Me, ITGLedgerMain.tbrMain, lACNew, lACEdit, lACDelete, , , , , , , True, True, , True
        Mode = Normal
        If rsHeader.RecordCount <> 0 Then
            Set FrmName = Me
            FormLocking True
            SetDataField
            SetDataSource
            rsHeader.Bookmark = vBookMark
        Else
            RSZero
        End If
    End If
    If Not OKUpdate Then
        MsgBox "Transaction update failed.", vbInformation, "ComUnion"
        cn.RollbackTrans
        connHeader.RollbackTrans
        connCriteria.RollbackTrans
        connApplyTo.RollbackTrans
        ErrorLog err.Number, err.Description, Me.Name 'Error log
    End If
    
End Sub

'Sets the form & recorset to add/edit mode
Public Sub TBEdit()
    Mode = AddNewEdit
    BitEnabled ITGLedgerMain, Me, ITGLedgerMain.tbrMain, , , , True, True, , , True, True, , , , True
    Set FrmName = Me
    FormLocking False
    ClrRequired &HC0&
    txtComID.Locked = True
    SSTab1.Tab = 0
    txtDescription.SetFocus
    vBM = rsHeader.Bookmark
End Sub

'Delete record
Public Sub TBDelete()
On Error GoTo ErrorHandler
    
    sID = Trim(txtComID)
    
    If rsHeader.RecordCount = 0 Then Exit Sub
    
    If MsgBox("Are you sure you want to delete this record?", vbQuestion + vbYesNo, "ComUnion") = vbNo Then Exit Sub
    
    GetChild
    If rsCriteria.RecordCount <> 0 Then
        rsCriteria.MoveFirst
        Do Until rsCriteria.RecordCount = 0
            rsCriteria.Delete adAffectCurrent
            If rsCriteria.RecordCount <> 0 Then rsCriteria.MoveFirst
        Loop
    End If
    If rsApplyTo.RecordCount <> 0 Then
        rsApplyTo.MoveFirst
        Do Until rsApplyTo.RecordCount = 0
            rsApplyTo.Delete adAffectCurrent
            If rsApplyTo.RecordCount <> 0 Then rsApplyTo.MoveFirst
        Loop
    End If
    
    'Audit trail
    UpdateLogFile "Commission Criteria", Trim(txtComID), "Deleted"

    rsHeader.Delete adAffectCurrent
    rsCriteria.UpdateBatch adAffectAll
    rsApplyTo.UpdateBatch adAffectAll
    rsHeader.UpdateBatch adAffectAll
    
    TBPrevRec

    Mode = Normal
    
    If rsHeader.RecordCount = 0 Then
        RSZero
    Else
        vBookMark = rsHeader.Bookmark
        UnbindControls
        SetDataSource
        SetDataField
        rsHeader.Bookmark = vBookMark
    End If

ErrorHandler:
    If err.Number = -2147217885 Then
        Resume Next
    ElseIf err.Number = -2147217864 Then
        cn.Execute "DELETE COMMISSION_CRITERIA WHERE cComID = '" & sID & "' AND cCompanyID = '" & COID & "'"
        cn.Execute "DELETE COMMISSION_APPLYTO WHERE cComID = '" & sID & "' AND cCompanyID = '" & COID & "'"
        cn.Execute "DELETE COMMISIION WHERE cComID = '" & sID & "' AND cCompanyID = '" & COID & "'"
        Resume Next
    End If

End Sub

'Search using the frmITGSearch
Public Sub TBFind()
    Mode = Normal
    txtComID.Locked = True
    frmITGSearch.Show
End Sub

'Search using the recordset primary key
Public Sub TBFindPrimary()
Dim sTemp As String
If Mode = Find Then
    FormWaitShow App.Path & "\Transmit.avi", "Loading data . . ."
            
    OpenNewConnection connHeader
    OpenNewConnection connCriteria
    OpenNewConnection connApplyTo
    
    Set rsHeader = Nothing
    Set rsCriteria = Nothing
    Set rsApplyTo = Nothing
    Set rsHeader = New ADODB.Recordset
    Set rsCriteria = New ADODB.Recordset
    Set rsApplyTo = New ADODB.Recordset
            
    sTemp = Trim$(GetSearchString)
    If sTemp = "ERROR" Then
        MsgBox "Only Allows A - Z, 0 - 9, '.', ',' and %(wildcard)", vbExclamation, ""
        BitEnabled ITGLedgerMain, Me, ITGLedgerMain.tbrMain, lACNew, , , , , , , , , True, , , True
        txtComID.Locked = False
        txtComID.SetFocus
        Exit Sub
    End If
            
    OpenRecordsetWithCN rsHeader, "*", "COMMISSION", connHeader, IIf(sTemp = "", "", " WHERE " & sTemp)
    OpenRecordsetWithCN rsCriteria, "*", "COMMISSION_CRITERIA", connCriteria
    OpenRecordsetWithCN rsApplyTo, "*", "COMMISSION_APPLYTO", connApplyTo
                
    Set FrmName = Me
    FormLocking True
            
    If rsHeader.RecordCount = 0 Then
        FormWaitHide
        MsgBox "No matching record/s found.", vbInformation, "ComUnion Search"
        RSZero
        FormSearch True
        SearchObj True
        Mode = Find
        Exit Sub
    End If
    SetDataSource
    SetDataField
    txtComID.BackColor = &HE0FFFF
    Mode = Normal
    BitEnabled ITGLedgerMain, Me, ITGLedgerMain.tbrMain, lACNew, lACEdit, lACDelete, , , , , , , True, True, , True

    FormWaitHide
Else
    RSZero
    FormSearch True
    SearchObj True
    Mode = Find
End If

End Sub
'Reload menu buttons (do not delete this sub)
Public Sub TBBitReload()
    BitVisible ITGLedgerMain.tbrMain, True
    ITGLedgerMain.tbrMain.Buttons("btnFind").ButtonMenus("btnFindP").Enabled = True
    BitReload ITGLedgerMain, Me, ITGLedgerMain.tbrMain, sBit
    Set FrmName = Me
    dtgName = dtgCriteria.Name
End Sub

'Close active window
Public Sub TBCloseWindow()
    Unload Me
End Sub

'Move first
Public Sub TBFirstRec()
    If rsHeader.State <> adStateOpen Then Exit Sub
    MoveFirst rsHeader
End Sub

'Move previous
Public Sub TBPrevRec()
    If rsHeader.State <> adStateOpen Then Exit Sub
    MovePrevious rsHeader
End Sub

'Move next
Public Sub TBNextRec()
    If rsHeader.State <> adStateOpen Then Exit Sub
    MoveNext rsHeader
End Sub

'Move last
Public Sub TBLastRec()
    If rsHeader.State <> adStateOpen Then Exit Sub
    MoveLast rsHeader
End Sub

'Add new line to the detail recordset
Public Sub TBNewLine()
    
    If rsHeader!cComID = "" Then Exit Sub
    
    If dtgName = "dtgCriteria" Then
        rsCriteria.AddNew
        rsCriteria!cCompanyID = COID
        rsCriteria!cComID = Trim(txtComID)
        GetChild
        dtgCriteria.SetFocus
        dtgCriteria.Col = 0
        rsCriteria.MoveLast
    ElseIf dtgName = "dtgApplyTo" Then
        rsApplyTo.AddNew
        rsApplyTo!cCompanyID = COID
        rsApplyTo!cComID = Trim(txtComID)
        GetChild
        dtgApplyTo.SetFocus
        dtgApplyTo.Col = 0
        rsApplyTo.MoveLast
    End If

End Sub

'Delete line in the detail recordset
Public Sub TBDeleteLine()
On Error GoTo ErrorHandler

    If dtgName = "dtgCriteria" Then
        If rsCriteria.RecordCount = 0 Then Exit Sub
        vBookMark = dtgCriteria.Bookmark
        GetChild
        dtgCriteria.Bookmark = vBookMark
        rsCriteria.Delete adAffectCurrent
    ElseIf dtgName = "dtgApplyTo" Then
        If rsApplyTo.RecordCount = 0 Then Exit Sub
        vBookMark = dtgApplyTo.Bookmark
        GetChild
        dtgApplyTo.Bookmark = vBookMark
        rsApplyTo.Delete adAffectCurrent
    End If
    GetChild

ErrorHandler:
    If err.Number = -2147217885 Then
        Resume Next
    End If

End Sub

'Undo all
Public Sub TBUndoLineAll()
On Error GoTo ErrorHandler

ErrorHandler:
    If err.Number = -2147217885 Then
        Resume Next
    End If
End Sub

'Undo current line
Public Sub TBUndoLineCurrent()
    MsgBox "Unavailable on " & Me.Name
End Sub

'Post current record
Public Sub TBPostRecord()
    MsgBox "Unavailable on " & Me.Name
End Sub

'Cancel current record
Public Sub TBCancelRecord()
    MsgBox "Unavailable on " & Me.Name
End Sub

'Print
Public Sub TBPrintRecord()
    MsgBox "Unavailable on " & Me.Name
End Sub

'Sets the data source of the controls
Sub SetDataSource()
    Set FrmName = Me
    BindControls rsHeader
    Set dtgCriteria.DataSource = rsCriteria
    Set dtgApplyTo.DataSource = rsApplyTo
    Set dtgList.DataSource = rsHeader
End Sub
    
'Sets the data field for every bounded controls
Sub SetDataField()
    With rsHeader
        txtComID.DataField = !cComID
        txtDescription.DataField = !cDescription
        txtType.DataField = !cType
    End With
End Sub

Private Sub rsHeader_MoveComplete(ByVal adReason As ADODB.EventReasonEnum, ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
On Error GoTo ErrorHandler
    
    If Not (rsHeader.EOF) Or Not (rsHeader.BOF) Then
        sbRS.Panels(1) = "Record: " & IIf((rsHeader.AbsolutePosition = -2), "0", rsHeader.AbsolutePosition) & "/" & rsHeader.RecordCount

        If rsHeader.Status <> adRecNew Then
            txtComID.Locked = True
        Else
            txtComID.Locked = False
        End If

        If Mode = AddNewEdit Then
            Select Case rsHeader.Status
                Case adRecNew
                    sbRS.Panels(2) = "New"
                Case adRecModified
                    sbRS.Panels(2) = "Modified"
                Case Else
                    sbRS.Panels(2) = ""
            End Select
        Else
            sbRS.Panels(2) = ""
        End If

        GetChild
    Else
        sbRS.Panels(1) = "Record: 0/0"
        sbRS.Panels(2) = ""
        txtComID.Locked = False
    End If

    If Mode = AddNewEdit Then
        dtgCriteria.Refresh
        dtgApplyTo.Refresh
    End If

ErrorHandler:
    'Err.Number -2147217885
    'Description - Row handle referred to a deleted row or a row marked for deletion.
    If err.Number = -2147217885 Then
        Resume Next
    End If
    
End Sub

Private Sub Timer1_Timer()
    If Mode = AddNewEdit Then
        SSTab1.TabEnabled(1) = False
    Else
        SSTab1.TabEnabled(1) = True
    End If
End Sub

Private Sub txtComID_LostFocus()
    'Does Code Already exists
    If Mode = AddNewEdit Then
        If Trim(txtComID) = "" Then
            MsgBox "Empty primary input.", vbInformation, "ComUnion"
            txtComID.SetFocus
        Else
            If rsHeader.Status <> adRecNew Then Exit Sub
            txtComID = Trim(txtComID)
            If IDExisting(rsHeader, "cComID", "COMMISSION", Trim(rsHeader!cComID)) Then
                MsgBox "Commission ID already exists.", vbInformation, "ComUnion"
                txtComID.SetFocus
            End If
        End If
    End If
End Sub

Private Sub txtType_Change()
    If Trim(txtType) <> "" Or txtType <> vbNullString Then
        cboType = Trim(txtType)
    Else
        cboType.ListIndex = -1
    End If
End Sub

'Check if all mandatory fields are complete
Function MandatoryOK() As Boolean
    
    MandatoryOK = True
    
    If Trim(txtComID) = "" Then
        MandatoryOK = False
        MsgBox "Field 'Commission ID' is mandatory. Null value is not allowed.", vbInformation, "ComUnion"
        txtComID.SetFocus
        Exit Function
    End If
    
    If rsHeader.Status = adRecNew Then
        If IDExisting(rsHeader, "cComID", "COMMISSION", Trim(rsHeader!cComID)) Then
            MandatoryOK = False
            MsgBox "Product ID already exists.", vbInformation, "ComUnion"
            txtComID.SetFocus
            Exit Function
        End If
    End If
    
    If Trim(txtDescription) = "" Then
        MandatoryOK = False
        MsgBox "Field 'Description' is mandatory. Null value is not allowed", vbInformation, "ComUnion"
        txtDescription.SetFocus
        Exit Function
    ElseIf Trim(txtType) = "" Then
        MandatoryOK = False
        MsgBox "Field 'Type' is mandatory. Null value is not allowed", vbInformation, "ComUnion"
        cboType.SetFocus
        Exit Function
    End If
    
    GetChild
    
    If rsApplyTo.RecordCount <> 0 Then rsApplyTo.MoveFirst
    Do Until rsApplyTo.EOF
        If rsApplyTo.Status = (adRecNew) Or rsApplyTo.Status = (adRecModified) Then
            If IsNull(rsApplyTo!cID) Then
                rsApplyTo.Delete
            ElseIf Trim(rsApplyTo!cID) = "" Then
                rsApplyTo.Delete
            Else
                rsApplyTo.MoveNext
            End If
        Else
            rsApplyTo.MoveNext
        End If
    Loop
    
    If rsCriteria.RecordCount <> 0 Then rsCriteria.MoveFirst
    Do Until rsCriteria.EOF
        If rsCriteria.Status = (adRecNew) Or rsCriteria.Status = (adRecModified) Then
            If IsNull(rsCriteria!cOperator) Then
                rsCriteria.Delete
            ElseIf Trim(rsCriteria!cOperator) = "" Then
                rsCriteria.Delete
            Else
                rsCriteria.MoveNext
            End If
        Else
            rsCriteria.MoveNext
        End If
    Loop

    GetChild
    
End Function

'Filter detail recordset to header's primary
Private Sub GetChild()
    rsApplyTo.Filter = "cComID = '" & Trim(rsHeader!cComID) & "'"
    rsCriteria.Filter = "cComID = '" & Trim(rsHeader!cComID) & "'"
End Sub

Private Function GetSearchString() As String
On Error GoTo ErrorHandler
Dim sWhere As String

    GetSearchString = True

    If Trim$(txtComID.Text) <> "" Then
        sWhere = sWhere & IIf(Trim$(sWhere) = "", "", " AND ") & " cComID LIKE '" & Trim$(txtComID.Text) & "%'"
    End If

    If Trim$(txtType.Text) <> "" Then
        sWhere = sWhere & IIf(Trim$(sWhere) = "", "", " AND ") & " cType LIKE '" & Trim$(txtType.Text) & "%'"
    End If
    
    GetSearchString = Trim$(sWhere)

    Exit Function
ErrorHandler:
    GetSearchString = "ERROR"
End Function

'Sets the form if record number is zero
Private Sub RSZero()
    sbRS.Panels(1) = "Record: 0/0"
    sbRS.Panels(2) = ""
    
    Set dtgCriteria.DataSource = Nothing
    Set dtgApplyTo.DataSource = Nothing
    dtgCriteria.Refresh
    dtgApplyTo.Refresh
    
    Set FrmName = Me
    UnbindControls
    TextClearing
    FormLocking True
    
    If rsHeader.State = adStateOpen Then rsHeader.Close
    If rsCriteria.State = adStateOpen Then rsCriteria.Close
    If rsApplyTo.State = adStateOpen Then rsApplyTo.Close
    
    BitEnabled ITGLedgerMain, Me, ITGLedgerMain.tbrMain, lACNew, , , , , , , , , True, , , True
    
    txtComID.Locked = False
    txtComID.SetFocus
    
    Mode = Find
End Sub

Sub SearchObj(lEnable As Boolean)
    cboType.Enabled = lEnable
End Sub

Public Sub AddDetailsFromList()
    
    GetChild
    
    With frmITGPickList.lvwList
        For i = 1 To .ListItems.Count
            If .ListItems(i).Checked = True Then
                rsApplyTo.AddNew
                rsApplyTo!cCompanyID = COID
                rsApplyTo!cComID = Trim(txtComID)
                If frmITGPickList.sColumnVariable = "Code" Then
                    rsApplyTo!cDesc = Trim(.ListItems(i).SubItems(1))
                    rsApplyTo!cID = Trim(.ListItems(i).Text)
                ElseIf frmITGPickList.sColumnVariable = "Desc" Then
                    rsApplyTo!cID = Trim(.ListItems(i).SubItems(1))
                    rsApplyTo!cDesc = Trim(.ListItems(i).Text)
                End If
            End If
        Next i
    End With

    GetChild
    
End Sub

